PHP & MySQL: Novice to Ninja by Kevin Yank
Author:Kevin Yank [Yank, Kevin]
Language: eng
Format: epub, mobi
ISBN: 9780987153081
Publisher: SitePoint Pty. Ltd.
Published: 2012-04-10T16:00:00+00:00
Note: Regular Expressions in Double Quoted Strings
All the regular expressions we’ve seen so far in this chapter have been expressed as single-quoted PHP strings. The automatic variable substitution provided by PHP strings is sometimes more convenient, but they can cause headaches when used with regular expressions. Double-quoted PHP strings and regular expressions share a number of special character escape codes. "\n" is a PHP string containing a newline character. Likewise, /\n/ is a regular expression that will match any string containing a newline character. We can represent this regular expression as a single-quoted PHP string ('/\n/') and all is well, because the code \n has no special meaning in a single-quoted PHP string. If we were to use a double-quoted string to represent this regular expression, we’d have to write "/\\n/"—with a double-backslash. The double-backslash tells PHP to include an actual backslash in the string, rather than combining it with the n that follows it to represent a newline character. This string will therefore generate the desired regular expression, /\n/. Because of the added complexity it introduces, it’s best to avoid using double-quoted strings when writing regular expressions. Note, however, that I have used double quotes for the replacement strings ("\n") passed as the second parameter to preg_replace. In this case, we actually do want to create a string containing a newline character, so a double-quoted string does the job perfectly.
With our line breaks all converted to newline characters, we can convert them to paragraph breaks (when they occur in pairs) and line breaks (when they occur alone): // Paragraphs $text = '<p>' . preg_replace('/\n\n/', '</p><p>', $text) . '</p>'; // Line breaks $text = preg_replace('/\n/', '<br>', $text);
Note the addition of <p> and </p> tags surrounding the joke text. Because our jokes may contain paragraph breaks, we must make sure the joke text is output within the context of a paragraph to begin with. This code does the trick: the line breaks in the text will now become the natural line- and paragraph-breaks expected by the user, removing the requirement to learn anything new to create this simple formatting. It turns out, however, that there’s a simpler way to achieve the same result in this case—there’s no need to use regular expressions at all! PHP’s str_replace function works a lot like preg_replace, except that it only searches for strings instead of regular expression patterns:$newString = str_replace(searchFor, replaceWith, oldString);
We can therefore rewrite our line-breaking code as follows: chapter8/includes/helpers.inc.php (excerpt)
// Convert Windows (\r\n) to Unix (\n) $text = str_replace("\r\n", "\n", $text); // Convert Macintosh (\r) to Unix (\n) $text = str_replace("\r", "\n", $text); // Paragraphs $text = '<p>' . str_replace("\n\n", '</p><p>', $text) . '</p>'; // Line breaks $text = str_replace("\n", '<br>', $text);
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Access | Data Mining |
Data Modeling & Design | Data Processing |
Data Warehousing | MySQL |
Oracle | Other Databases |
Relational Databases | SQL |
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Azure Data and AI Architect Handbook by Olivier Mertens & Breght Van Baelen(6411)
Building Statistical Models in Python by Huy Hoang Nguyen & Paul N Adams & Stuart J Miller(6370)
Serverless Machine Learning with Amazon Redshift ML by Debu Panda & Phil Bates & Bhanu Pittampally & Sumeet Joshi(6259)
Data Wrangling on AWS by Navnit Shukla | Sankar M | Sam Palani(6035)
Driving Data Quality with Data Contracts by Andrew Jones(5999)
Learning SQL by Alan Beaulieu(5959)
Machine Learning Model Serving Patterns and Best Practices by Md Johirul Islam(5766)
Weapons of Math Destruction by Cathy O'Neil(5722)
Big Data Analysis with Python by Ivan Marin(5175)
Data Engineering with dbt by Roberto Zagni(4200)
Solidity Programming Essentials by Ritesh Modi(3839)
Time Series Analysis with Python Cookbook by Tarek A. Atwan(3690)
Pandas Cookbook by Theodore Petrou(3407)
Blockchain Basics by Daniel Drescher(3274)
Hands-On Machine Learning for Algorithmic Trading by Stefan Jansen(2885)
Feature Store for Machine Learning by Jayanth Kumar M J(2797)
Learn T-SQL Querying by Pam Lahoud & Pedro Lopes(2779)
Mastering Python for Finance by Unknown(2731)
